package org.github.manx98.jlibhac.hle;

import lombok.Data;
import org.github.manx98.jlibhac.fs.IStorage;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/6 0:03
 */
@Data
public class ServiceAccessControl {
    Map<String, Boolean> services = new HashMap<>();

    public ServiceAccessControl(IStorage stream, int offset, int size) throws IOException {
        stream.setPosition(offset);

        int bytesRead = 0;

        while (bytesRead != size) {
            byte controlByte = stream.readByte();

            if (controlByte == 0) {
                break;
            }

            int length = (controlByte & 0x07) + 1;
            boolean registerAllowed = (controlByte & 0x80) != 0;

            services.put(new String(stream.readBytes(length)), registerAllowed);

            bytesRead += length + 1;
        }
    }
}
